package pl.world.actors
{
	import pl.animations.Animations;
	import pl.world.actors.proto.BaseActor;

	import starling.animation.Juggler;
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.display.MovieClip;
	import starling.display.Sprite;

	import flash.geom.Point;

	/**
	 * @author michalr
	 */
	public class AnimatedActor extends BaseActor implements IAnimatedActor
	{
		private var juggler:Juggler;
		private var right:MovieClip;
		private var bottomRight:MovieClip;
		private var bottom:MovieClip;
		private var bottomLeft:MovieClip;
		private var left:MovieClip;
		private var topRight:MovieClip;
		private var topLeft:MovieClip;
		private var top:MovieClip;
		private var currentAnimation:MovieClip;

		public function AnimatedActor( _x:uint, _y:uint, _type:uint, atlasID:uint, speed:Number )
		{
			super( _x, _y, _type, atlasID, speed );

			bottom = Animations.getRobotAnimation( "move_down_" );
			bottomRight = Animations.getRobotAnimation( "move_bottomRight_" );
			right = Animations.getRobotAnimation( "move_right_" );
			bottomLeft = Animations.getRobotAnimation( "move_bottomLeft_" );
			left = Animations.getRobotAnimation( "move_left_" );
			topRight = Animations.getRobotAnimation( "move_topRight_" );
			topLeft = Animations.getRobotAnimation( "move_topLeft_" );
			top = Animations.getRobotAnimation( "move_top_" );

			juggler = new Juggler();
		}

		override public function processLogic():void
		{
			super.processLogic();
		}

		override public function get display():DisplayObject
		{
			if (_display == null)
			{
				_display = new Sprite();
				if (currentAnimation != null)
				{
					changeDisplayAnimation( currentAnimation );
					currentAnimation.pause();
				}
			}
			return _display;
		}

		override public function cleanView():void
		{
			while ((_display as Sprite).numChildren > 0)
				(_display as Sprite).removeChildAt( 0 );
			_display.dispose();
			_display = null;
		}

		override protected function viewFaceingAngleChanged():void
		{
			var p:Point = (path[1] as Point).clone();
			p.x -= (path[0] as Point).x;
			p.y -= (path[0] as Point).y;

			var angle:Number = Math.atan2( p.y, p.x );
			var angleD:Number = angle * 180 / Math.PI - 45;

			if (angleD > -110 && angleD < -70 )
			{
				if (currentAnimation != top)
					changeDisplayAnimation( top );
			}
			else if (angleD > -160 && angleD < -110)
			{
				if (currentAnimation != topLeft)
					changeDisplayAnimation( topLeft );
			}
			else if (angleD > -225 && angleD < -160)
			{
				if (currentAnimation != left)
					changeDisplayAnimation( left );
			}
			else if (angleD > 90 )
			{
				if (currentAnimation != bottomLeft)
					changeDisplayAnimation( bottomLeft );
			}
			else if (angleD > 70 && angleD < 110 )
			{
				if (currentAnimation != bottom)
					changeDisplayAnimation( bottom );
			}
			else if (angleD > 20 && angleD < 70)
			{
				if (currentAnimation != bottomRight)
					changeDisplayAnimation( bottomRight );
			}
			else if (angleD > -20 && angleD < 20)
			{
				if (currentAnimation != right)
					changeDisplayAnimation( right );
			}
			else if (angleD > -70 && angleD < 20)
			{
				if (currentAnimation != topRight)
					changeDisplayAnimation( topRight );
			}
		}

		override protected function onAllPathFinished():void
		{
			if (currentAnimation != null) currentAnimation.pause();
		}

		override protected function onPathIsUpdated():void
		{
			if (currentAnimation != null) currentAnimation.play();
		}

		private function changeDisplayAnimation( toreAdd:MovieClip ):void
		{
			if (isRendered == true)
			{
				while ((_display as Sprite).numChildren > 0)
					(_display as Sprite).removeChildAt( 0 );

				if (currentAnimation != null)
					Starling.juggler.remove( currentAnimation );

				Starling.juggler.add( toreAdd );

				(_display as Sprite).addChild( toreAdd );
				toreAdd.y = -toreAdd.height;

				currentAnimation = toreAdd;
				currentAnimation.play();
				updateView = true;
			}
		}
	}
}
